﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAL;
using Models;

namespace word.winform
{
    public partial class AddGoods : Form
    {
        Dictionary<int, String> CBDic = new Dictionary<int, string>();

        public AddGoods()
        {
            InitializeComponent();
        }

        

        private void btn_ok_Click(object sender, EventArgs e)
        {
            try
            {
                GoodsModel goodsModel = new GoodsModel();
                goodsModel.g_Name = TB_GName.Text.ToString();
                goodsModel.t_ID = getTID(CB_GType.SelectedItem.ToString()).ToString();
                goodsModel.g_Price = float.Parse(TB_GPrice.Text.ToString());
                goodsModel.g_Discount = float.Parse(TB_GDiscount.Text.ToString());
                goodsModel.g_Number = int.Parse(TB_Number.Text.ToString());
                goodsModel.g_ProduceDate = DateTime.Parse(Time.Value.ToShortDateString());
                goodsModel.g_Image = TB_Image.Text.ToString();
                goodsModel.g_Status = TB_GState.Text.ToString();
                goodsModel.g_Description = TB_GDescribe.Text.ToString();
                if (DAL.AddGoods.insert(goodsModel))
                {
                    MessageBox.Show("商品添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("商品添加失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch
            {
                MessageBox.Show("请输入正确完整的信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            


        }


        /// <summary>
        /// 加载商品类别的数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddGoods_Load(object sender, EventArgs e)
        {
            String sql = "select t_ID,t_Name from Types";
            DataTable dataTable = DBHelper.Select(sql);
            foreach (DataRow item in dataTable.Rows)
            {
                CBDic.Add((int)item["t_ID"], (string)item["t_Name"]);
                CB_GType.Items.Add((string)item["t_Name"]);
            }

        }

        public int getTID(String type)
        {
            foreach (KeyValuePair<int,String> item in CBDic)
            {
                if (type == item.Value)
                {
                    return item.Key;
                }
            }
            return 0;
        }

        private void btn_canel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
